package com.yisu.springboot.listener;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

/**
 * @description: 创建一个消息监听器（Message Listener）来订阅Redis的某个频道（channel），并处理接收到的消息。
 * @author: xuyisu
 * @date: 2024-08-27
 */
@Component
public class RedisMessageListener implements MessageListener {

    @Override
    public void onMessage(Message message, byte[] pattern) {
        // message.getBody() 是消息体，message.getChannel() 是频道
        String messageReceived = new String(message.getBody());
        String channel = new String(message.getChannel());
        System.out.println("Received <" + messageReceived + "> from channel <" + channel + ">");
    }
}
